const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    ispwd:false,
    number:"",
    userInfo:'',
    pwd:["","","","","",""],
    code:'',
    mobile:'',
    timer: '', //定时器名字
    countDownNum: '60', //倒计时初始值
    codeDis: false,
    telcode: '发送验证码',
    uuid:'',
    isfocus:false,
    iscode:false,
  },
  onShow: function (option) {
    this.info()
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          userInfo:res.data.baseData,
          mobile:res.data.baseData.mobile
        })
        if(!this.data.userInfo.payPassword){
          this.setData({
            ispwd:true
          })
        }
      }
    })
  },
  countDown: function() {
    let that = this;
    let countDownNum = that.data.countDownNum;
    that.setData({
      timer: setInterval(function() {
        countDownNum--;
        that.setData({
          countDownNum: countDownNum,
          codeDis: true
        })
        if (countDownNum <= 0) {
          that.setData({
            codeDis: false,
            countDownNum: 60
          })
          clearInterval(that.data.timer);
        }
      }, 1000)
    })
  },
  //发送短信验证码
  sendCode: function() {
    if (this.data.mobile == '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!(/^1[123456789]\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    // 获取数据
    Controller.sendChangeCode({
      mobile: this.data.mobile
    }).then(res => {
      this.setData({
        uuid:res.data.baseData.uuid
      })
      wx.showToast({
        title: '发送成功！',
        icon: 'success',
        duration: 1500
      })
      this.countDown();
    });
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
    
  },
  
  changepwd(e){
    var number=e.detail.value;
    var pwd=number.split('');
    if(number){
      this.setData({
        isfocus:true
      })
    }
    if(pwd.length>6){
      return
    }
    if(pwd.length<6){
      for(var i=(6-pwd.length);i>0;i--){
        pwd.push("")
      }
    }
    this.setData({
      number,
      pwd
    })
    if(this.data.userInfo && this.data.userInfo.payPassword){
      // if(number.length>=6){
      //   this.setData({
      //     ispwd:false,
      //     iscode:true,
      //     isfocus:false
      //   })
      // }
    }else{
      if(number.length>=6){
        this.setPayPassword()
      }
    }
    
  },
  closepwd(){
    this.setData({
      ispwd:false,
      isfocus:false,
      number:"",
      pwd:["","","","","",""],
    })
    wx.navigateBack({
      delta: 1,  // 返回上一级页面。
      success: function() {
        console.log('成功！')
      }
    })
  },
  repair(){
    if(!this.data.code){
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    this.setData({
      ispwd:true,
    })
  },
  inputblur(){
    this.setData({
      isfocus:false
    })
  },
  save(){
    if(this.data.number.length<6){
      wx.showToast({
        title: '请输入6位密码',
        icon: 'none'
      })
      return
    }
    if(this.data.userInfo.payPassword){
      this.changePayPassword()
    }else{
      this.setPayPassword()
    }
    
  },
  setPayPassword(){
    Controller.setPayPassword({
      password: this.data.number
    }).then(res => {
      wx.showToast({
        title: '设置成功！',
        icon: 'success',
        duration: 1500
      })
      this.setData({
        isfocus:false
      })
      var that=this;
      setTimeout(()=>{
        that.closepwd()
      },1000)
    });
  },
  changePayPassword(){
    Controller.changePayPassword({
      password: this.data.number,
      code:this.data.code,
      uuid:this.data.uuid
    }).then(res => {
      wx.showToast({
        title: '设置成功！',
        icon: 'success',
        duration: 1500
      })
      var that=this;
      setTimeout(()=>{
        that.closepwd()
      },1000)
    }).catch(res=>{
      wx.showToast({
        title:res.msg,
        icon: 'none',
        duration: 1500
      })
      this.setData({
        ispwd:false,
        isfocus:false,
        number:"",
        pwd:["","","","","",""],
      })
    })
  },

})